/**
 * 
 */
package com.et114.modules.content.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.util.List;

import com.et114.components.simpletable.DataList;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.utility.NumberUtils;
import com.et114.modules.content.services.CommonContentImpl;
import com.et114.modules.content.vo.CommonContent;
import com.et114.modules.content.vo.CommonType;
import com.et114.modules.products.commons.Const;

/*
 * @author guanhw
 */
public class CommonContentAction extends BaseDispatchAction {
	public  ActionForward getCommonContent( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		Long typeId = contentForm.getTypeId ( ) ; 
		Long contentId = contentForm.getContentId ( ) ; 
		
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		
		if ( NumberUtils.isEmptyOrZero ( typeId ) ) { 
			throw new Exception ( "typeId is NULL !" ) ; 
		}
		
		if ( !NumberUtils.isEmptyOrZero ( contentId ) ) {  // load one
			commonContent = cci.getCommonContentById ( contentId ) ; 
			request.setAttribute ( "vo" , commonContent ) ; 
			return mapping.findForward ( "commonContent" ) ; 
		}
		else { //loads 
			DataList dl = cci.getCommonContents ( commonContent ) ; 
			request.setAttribute ( "searchvo" , commonContent ) ; 
			request.setAttribute ( "dl" , dl ) ; 
			return mapping.findForward ( "commonContents" ) ; 
		}
	}
	
	public  ActionForward toEditCommonContent( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		Long typeId = contentForm.getTypeId ( ) ;  
		if ( NumberUtils.isEmptyOrZero ( typeId ) ) { 
			throw new Exception ( "typeId is NULL !" ) ; 
		}
		
		request.setAttribute ( "vo" , commonContent ) ; 
		return mapping.findForward ( "commonContent" ) ; 
	}
	
	
	public  ActionForward editCommonContent( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		Long typeId = contentForm.getTypeId ( ) ; 
		Long contentId = contentForm.getContentId ( ) ; 
		
		commonContent.setUpdtUser ( getCurrentUser ( request ).getUserId ( ) ) ;
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		if ( NumberUtils.isEmptyOrZero ( typeId ) ) { 
			throw new Exception ( "typeId is NULL !" ) ; 
		}
		
		CommonType commonType = cci.getCommonTypeById ( typeId ) ; 
		if ( NumberUtils.isEmptyOrZero ( contentId ) ) {  //new 
			contentId = cci.saveCommonContent ( commonContent ) ; 
		}
		else {
			cci.updateCommonContent ( commonContent ) ; 
		}
		
		
		return new ActionForward ( "cc.go?method=getCommonContent&typeId="
				+ typeId + "&contentId=" + contentId + "&globalKey="
				+ commonType.getGlobalKey ( ) , true );
	}
	
	
	public  ActionForward removeCommonContent( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		Long typeId = contentForm.getTypeId ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( typeId ) ) { 
			throw new Exception ( "typeId is NULL !" ) ; 
		}
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ;
		
		CommonType commonType = cci.getCommonTypeById ( typeId ) ; 
		
		Long[] deleteIds = contentForm.getDeleteId ( ) ; 
		if ( deleteIds != null  ) {
			for ( Long deleteId : deleteIds ) {
				cci.removeCommonContent ( deleteId ) ;
			}
		}
		 
		return new ActionForward ( "cc.go?method=getCommonContent&typeId="
				+ typeId + "&globalKey=" + commonType.getGlobalKey ( ) , true );

	}
	
	public  ActionForward faqRightTen4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 

		CommonType commonType = new CommonType( ) ; 
		commonType.setGlobalKey ( Const.CONTENT_NEWS_TYPE_GLOBAL_FAQ ) ; 

		List< CommonContent > dl = cci.getCommonContentsNpageCacheModel ( commonType ) ; 
		request.setAttribute ( "faqtens" , dl ) ; 
		return mapping.findForward ( "faqt4View" ) ; 
	}
	
	
	
	
	public  ActionForward newsCenter4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		//commonContent.setTypeId ( Const.CONTENT_NEWS_TYPE_NEWSCENTER ) ; 
		
		CommonType commonType = new CommonType( ) ; 
		commonType.setGlobalKey ( Const.CONTENT_NEWS_TYPE_GLOBAL_NEWS ) ; 
		commonType.setTypeId ( Const.CONTENT_NEWS_TYPE_NEWSCENTER ) ; 
		
		DataList dl = cci.getCommonContentsCacheModel ( commonType ) ; 
		request.setAttribute ( "dl" , dl ) ; 
		return mapping.findForward ( "news4View" ) ; 
	}
	
	
	
	
	public  ActionForward newCenter4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		commonContent.setTypeId ( Const.CONTENT_NEWS_TYPE_NEWSCENTER ) ; 
		
		Long contentId = commonContent.getContentId ( ) ; 
		
		commonContent = cci.getCommonContentByIdCacheModel ( contentId ) ; 
		request.setAttribute ( "vo" , commonContent ) ; 
		return mapping.findForward ( "new4View" ) ; 
	}	
	
	
	
	public  ActionForward qsCenter4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		
		CommonType commonType = new CommonType( ) ; 
		commonType.setGlobalKey ( Const.CONTENT_NEWS_TYPE_GLOBAL_FAQ ) ; 
		List< CommonType > roots = cci.getRootCommonTypesCacheModel( commonType ) ; 
		request.setAttribute ( "roots" , roots ) ;
		
		
		
		if ( !NumberUtils.isEmptyOrZero ( commonContent.getTypeId ( ) ) ) {  //loads 
			commonType.setTypeId ( commonContent.getTypeId ( ) ) ;
		}
		
		
		DataList dl = cci.getCommonContentsCacheModel ( commonType ) ; 
		request.setAttribute ( "dl" , dl ) ; 
		request.setAttribute ( "searchVo" , commonContent ) ;
		return mapping.findForward ( "qs4View" ) ; 
	}
	
	
	
	
	public  ActionForward qCenter4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		//commonContent.setTypeId ( Const.CONTENT_NEWS_TYPE_NEWSCENTER ) ; 
		
		Long contentId = commonContent.getContentId ( ) ; 
		
		commonContent = cci.getCommonContentByIdCacheModel ( contentId ) ; 
		request.setAttribute ( "vo" , commonContent ) ; 
		return mapping.findForward ( "new4View" ) ; 
	}	
	
	
	public  ActionForward ssCenter4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		
		CommonType commonType = new CommonType( ) ; 
		commonType.setGlobalKey ( Const.CONTENT_NEWS_TYPE_GLOBAL_SERVICE ) ; 
		commonType.setTypeId ( Const.CONTENT_NEWS_TYPE_SERVICE ) ; 
		
		List< CommonContent > dl = cci.getCommonContentsNpageCacheModel ( commonType ) ; 
		request.setAttribute ( "dl" , dl ) ; 
		return mapping.findForward ( "ss4View" ) ; 
	}
	
	public  ActionForward sRightTen4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 

		CommonType commonType = new CommonType( ) ; 
		commonType.setGlobalKey ( Const.CONTENT_NEWS_TYPE_GLOBAL_SERVICE ) ; 

		List< CommonContent > dl = cci.getCommonContentsNpageCacheModel ( commonType ) ; 
		request.setAttribute ( "stens" , dl ) ; 
		return mapping.findForward ( "st4View" ) ; 
	}	
	
	
	public  ActionForward sCenter4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		
		CommonContentForm contentForm = ( CommonContentForm ) form ;
		CommonContent commonContent = new  CommonContent( ) ; 
		
		PropertyUtils.copyProperties ( commonContent , contentForm ) ; 
		CommonContentImpl cci = ( CommonContentImpl )getBean( "commonContentImpl" ) ; 
		commonContent.setTypeId ( Const.CONTENT_NEWS_TYPE_SERVICE ) ; 
		
		Long contentId = commonContent.getContentId ( ) ; 
		
		commonContent = cci.getCommonContentByIdCacheModel ( contentId ) ; 
		request.setAttribute ( "vo" , commonContent ) ; 
		return mapping.findForward ( "s4View" ) ; 
	}		
}
